package com.china08.yxyapi.api.manage.tec.teaching.lesson;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicCatalogRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsCatalogListController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolCatalogRepository schoolCatalogRepository;

    @Autowired
    YxyLessonsTopicCatalogRepository topicCatalogRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/catalog/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String schoolId = getProfile(request, response).getSchoolId();
        String lessonType = reqModel.lessonType;
        String catalogId1 = reqModel.catalogId1;
        String catalogId2 = reqModel.catalogId2;
        Integer level = reqModel.level;

        List<RespModel> returnList = new ArrayList<>();
        // 获取校本课程目录
        if ("1".equals(lessonType)) {
            List<YxyLessonsSchoolCatalog> catalogList = catalog1(catalogId1, schoolId, level);
            for (YxyLessonsSchoolCatalog catalog : catalogList) {
                RespModel respModel = new RespModel();
                respModel.name = catalog.getName();
                respModel.id = catalog.getId();
                returnList.add(respModel);
            }
        }

        // 专题课程目录
        if ("2".equals(lessonType)) {
            List<YxyLessonsTopicCatalog> topicList = catalog2(catalogId1, catalogId2, level);
            for (YxyLessonsTopicCatalog topic : topicList) {
                RespModel respModel = new RespModel();
                respModel.name = topic.getName();
                respModel.id = topic.getId();
                returnList.add(respModel);
            }
        }
        return returnList;
    }

    public List<YxyLessonsSchoolCatalog> catalog1(String catalogId1, String schoolId, Integer level) {
        if (level == 1) {
            return schoolCatalogRepository.findByPidAndSchoolIdOrderByOrderNumAsc("0", schoolId);
        } else {
            return schoolCatalogRepository.findByPidAndSchoolIdOrderByOrderNumAsc(catalogId1, schoolId);
        }
    }

    public List<YxyLessonsTopicCatalog> catalog2(String catalogId1, String catalogId2, Integer level) {
        if (level == 1) {
            return topicCatalogRepository.findByPidOrderByOrderNumAsc("0");
        } else if (level == 2) {
            return topicCatalogRepository.findByPidOrderByOrderNumAsc(catalogId1);
        } else {
            return topicCatalogRepository.findByPidOrderByOrderNumAsc(catalogId2);
        }
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonType;
        public String catalogId1;
        public String catalogId2;
        public Integer level;

        public void check() {
            Assert.isTrue(StringUtils.isNotBlank(lessonType), "课程分类为空");
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public String id;
        public String name;
    }
}
